﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvansRS.Domain;
using AvansRS.Domain.DataInterface;
using NHibernate;
using NHibernate.Criterion;

namespace AvansRS.Data
{
    public class AccountRepository :  BaseRepository<Account>, IAccountRepository
    {        
        public Account FindByUsername(string username)
        {
            using (ISession session = SessionProvider.GetSession())
            {
                Account domainObject = session.CreateCriteria(typeof(Account))
                   .Add(Expression.Eq("UserName", username))
                   .UniqueResult<Account>();

                return domainObject;
            }
        }

        public bool ValidateAccount(string username, string password)
        {
            using (ISession session = SessionProvider.GetSession())
            {
                Account domainObject = session.CreateCriteria(typeof(Account))
                   .Add(Expression.Eq("UserName", username))
                   .Add(Expression.Eq("Password", password))
                   .UniqueResult<Account>();

                if (domainObject == null)
                    return false;
                return true;                
            }
        }

        public Account FindByResetKey(string key)
        {
            using (ISession session = SessionProvider.GetSession())
            {
                Account domainObject = session.CreateCriteria(typeof(Account))
                   .Add(Expression.Eq("ResetKey", key))
                   .UniqueResult<Account>();

                return domainObject;
            }
        }

        public Account FindByEmail(string email)
        {
            using (ISession session = SessionProvider.GetSession())
            {
                Account domainObject = session.CreateCriteria(typeof(Account))
                   .Add(Expression.Eq("Email", email))
                   .UniqueResult<Account>();

                return domainObject;
            }
        }
    }
}
